package com.xuyiwei.xblog.core.util;

import java.io.UnsupportedEncodingException;
import java.util.Random;

/**
 * Created by xuyiwei on 2018/10/15.
 */
public class StringUtil {

    /**
     * 首字母变小写
     */
    public static String firstCharToLowerCase(String str) {
        char firstChar = str.charAt(0);
        if (firstChar >= 'A' && firstChar <= 'Z') {
            char[] arr = str.toCharArray();
            arr[0] += ('a' - 'A');
            return new String(arr);
        }
        return str;
    }

    /**
     * 首字母变大写
     */
    public static String firstCharToUpperCase(String str) {
        char firstChar = str.charAt(0);
        if (firstChar >= 'a' && firstChar <= 'z') {
            char[] arr = str.toCharArray();
            arr[0] -= ('a' - 'A');
            return new String(arr);
        }
        return str;
    }

    /**
     * 字符串为 null 或者内部字符全部为 ' ' '\t' '\n' '\r' 这四类字符时返回 true
     */
    public static boolean isBlank(String str) {
        if (str == null) {
            return true;
        }
        int len = str.length();
        if (len == 0) {
            return true;
        }
        if("null".equalsIgnoreCase(str)){
            return true;
        }
        for (int i = 0; i < len; i++) {
            switch (str.charAt(i)) {
                case ' ':
                case '\t':
                case '\n':
                case '\r':
                    // case '\b':
                    // case '\f':
                    break;
                default:
                    return false;
            }
        }
        return true;
    }

    public static boolean notBlank(String str) {
        return !isBlank(str);
    }

    public static boolean notBlank(String... strings) {
        if (strings == null) {
            return false;
        }
        for (String str : strings) {
            if (isBlank(str)) {
                return false;
            }
        }
        return true;
    }

    public static boolean notNull(Object... paras) {
        if (paras == null) {
            return false;
        }
        for (Object obj : paras) {
            if (obj == null) {
                return false;
            }
        }
        return true;
    }

    public static String toCamelCase(String stringWithUnderline) {
        if (stringWithUnderline.indexOf('_') == -1) {
            return stringWithUnderline;
        }

        stringWithUnderline = stringWithUnderline.toLowerCase();
        char[] fromArray = stringWithUnderline.toCharArray();
        char[] toArray = new char[fromArray.length];
        int j = 0;
        for (int i = 0; i < fromArray.length; i++) {
            if (fromArray[i] == '_') {
                // 当前字符为下划线时，将指针后移一位，将紧随下划线后面一个字符转成大写并存放
                i++;
                if (i < fromArray.length) {
                    toArray[j++] = Character.toUpperCase(fromArray[i]);
                }
            } else {
                toArray[j++] = fromArray[i];
            }
        }
        return new String(toArray, 0, j);
    }

    public static String join(String[] stringArray) {
        StringBuilder sb = new StringBuilder();
        for (String s : stringArray) {
            sb.append(s);
        }
        return sb.toString();
    }

    public static String join(String[] stringArray, String separator) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < stringArray.length; i++) {
            if (i > 0) {
                sb.append(separator);
            }
            sb.append(stringArray[i]);
        }
        return sb.toString();
    }

    public static boolean slowEquals(String a, String b) {
        byte[] aBytes = (a != null ? a.getBytes() : null);
        byte[] bBytes = (b != null ? b.getBytes() : null);
        return HashUtil.slowEquals(aBytes, bBytes);
    }

    public static String getRandomUUID() {
        return java.util.UUID.randomUUID().toString().replace("-", "");
    }

    public static boolean isUTF8(String str){
        if(StringUtil.notBlank(str)){
            try {
                str.getBytes("utf-8");
                return true;
            } catch (UnsupportedEncodingException e) {
                return false;
            }
        }else{
            return false;
        }
    }

    /**
     * 处理sql语句in里面的函数 因为String类型的逗号隔开不带'' 意思是把11,22处理成'11','22'这样的函数类型
     *
     * @param param
     * @return
     */
    public static String sqlInTemplate(String param) {
        if (StringUtil.notBlank(param)) {
            String paramSql = "";
            String[] paramArr = param.split(",");
            for (int i = 0; i < paramArr.length; i++) {
                if (i < (paramArr.length - 1)) {
                    paramSql += "'" + paramArr[i] + "',";
                } else {
                    paramSql += "'" + paramArr[i]+ "'";

                }
            }
            return paramSql;
        } else {
            return null;
        }
    }

    /**
     * 处理sql语句in里面的函数 因为String类型的逗号隔开不带'' 意思是把11,22处理成'11','22'这样的函数类型
     *
     * @param param
     * @return
     */
    public static StringBuffer append(StringBuffer param,String append) {
        if(StringUtil.notBlank(append)){
            return param;
        }else{
            if (StringUtil.notBlank(param.toString())) {
                return param.append(",").append(append);
            } else {
                return param.append(append);
            }
        }
    }

    /**
     * //生成6位随机数,测试多次后有产生size位随机数的bug
     */
    public static int randomCode(int size) {
        StringBuilder str = new StringBuilder();
        Random random = new Random();
        for (int i = 0; i < size; i++) {
            str.append(random.nextInt(10));
        }
        return Integer.parseInt(str.toString());
    }

}
